package com.example.crm.controller;

import com.example.crm.entity.CustomerTag;
import com.example.crm.service.CustomerTagService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 客户标签控制器
 */
@RestController
@RequestMapping("/api/customer/tag")
public class CustomerTagController {

    @Autowired
    private CustomerTagService customerTagService;

    /**
     * 获取所有标签
     */
    @GetMapping("/list")
    public Result<List<CustomerTag>> listAllTags() {
        List<CustomerTag> tags = customerTagService.listAllTags();
        return Result.success(tags);
    }

    /**
     * 获取客户的标签
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<CustomerTag>> getTagsByCustomerId(@PathVariable Long customerId) {
        List<CustomerTag> tags = customerTagService.getTagsByCustomerId(customerId);
        return Result.success(tags);
    }

    /**
     * 为客户设置标签
     */
    @PostMapping("/customer/{customerId}")
    public Result<Boolean> setCustomerTags(@PathVariable Long customerId, @RequestBody List<Long> tagIds) {
        boolean result = customerTagService.setCustomerTags(customerId, tagIds);
        return Result.success(result);
    }

    /**
     * 新增标签
     */
    @PostMapping("/add")
    public Result<Boolean> addTag(@RequestBody CustomerTag tag) {
        tag.setCreateTime(LocalDateTime.now());
        tag.setUpdateTime(LocalDateTime.now());
        tag.setDeleted(0);
        boolean result = customerTagService.save(tag);
        return Result.success(result);
    }

    /**
     * 更新标签
     */
    @PostMapping("/update")
    public Result<Boolean> updateTag(@RequestBody CustomerTag tag) {
        tag.setUpdateTime(LocalDateTime.now());
        boolean result = customerTagService.updateById(tag);
        return Result.success(result);
    }

    /**
     * 删除标签
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteTag(@PathVariable Long id) {
        boolean result = customerTagService.removeById(id);
        return Result.success(result);
    }
} 